package leecode

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
	//当节点为p , q 或 nil 时, 递归停止
	if root == p || root == q || root == nil {
		return root
	}
	treeLeft := lowestCommonAncestor(root.Left, p, q)
	treeRiht := lowestCommonAncestor(root.Right, p, q)
	if treeLeft != nil && treeRiht != nil {
		return root
	}
	if treeLeft == nil && treeRiht != nil {
		return treeRiht
	} else if treeLeft != nil && treeRiht == nil {
		return treeLeft
	}
	return nil
}
